package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.user.dto.response.BorrowerInfoResponseDTO;
import com.niiwoo.civet.user.dto.response.credit.*;
import com.niiwoo.civet.user.service.TianchengDataDubboService;
import com.niiwoo.civet.user.service.local.TianchengDataService;
import com.niiwoo.tripod.tiancheng.component.TianchengBorrowerInfoService;
import com.niiwoo.tripod.tiancheng.enums.FunctionCodeEnum;
import com.niiwoo.tripod.tiancheng.request.ds.BorrowerInfoRequest;
import com.niiwoo.tripod.tiancheng.response.ds.BorrowerInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CompletableFuture;

/**
 * 天秤数据服务
 * Created by dell on 2017/12/6.
 * Description：civet-parent
 */
@Slf4j
@Service(version = "1.0.0")
public class TianchengDataDubboServiceImpl implements TianchengDataDubboService {

    @Autowired
    private TianchengBorrowerInfoService tianchengBorrowerInfoService;
    
    @Autowired
    private TianchengDataService tianchengDataService;


    /**
     * 请求天秤借款人信息
     * @param mobilePhone
     * @param realName
     * @param identityCard
     */
    public BorrowerInfoResponseDTO postBorrowerInfo(String mobilePhone, String realName, String identityCard,String userId){
        log.error("请求天秤借款人信息 mobilePhone：{}，realName{}，identityCard：{}", mobilePhone, realName, identityCard);
        BorrowerInfoRequest request = new BorrowerInfoRequest();
        request.setMobilePhone(mobilePhone);
        request.setRealName(realName);
        request.setIdentityCard(identityCard);

        BorrowerInfoResponseDTO responseDTO = new BorrowerInfoResponseDTO();
        try {
            String functionCode = FunctionCodeEnum.LIBRA_CREDIT_CARD_LOAN_TWO.getHeadFunctionCode();
            CreditScoreCardDTO creditScoreCardDTO = tianchengDataService.queryCreditScoreCardParam(userId,functionCode);
            if (creditScoreCardDTO != null){
                request.setFromSource("1"); //1：信用卡贷；2：卡牛
                request.setLoginName(creditScoreCardDTO.getYx_login_name());
                request.setEntryId(creditScoreCardDTO.getYx_entry_id());
            }
            CompletableFuture<BorrowerInfoResponse> future = tianchengBorrowerInfoService.postBorrowerInfo(request);
            BorrowerInfoResponse response = future.get();
            log.info("请求天秤借款人信息，请求结果：{}", JSON.toJSONString(response));
            BeanUtils.copyProperties(response, responseDTO);
        } catch (Exception e) {
            log.error("请求天秤借款人信息出错! mobilePhone：{}，realName{}，identityCard：{}", mobilePhone, realName, identityCard, e);
        }
        return responseDTO;
    }

	@Override
	public TianchengCreditBaseInfoResponseDTO getUserTianchengCreditBaseInfo(String userId) {
		TianchengCreditBaseInfoResponseDTO dto = new TianchengCreditBaseInfoResponseDTO();
		
		dto.setBaseInfo(tianchengDataService.getBasicInfo(userId));
		dto.setWorkInfo(tianchengDataService.getWorkInfo(userId));
		dto.setContacts(tianchengDataService.getContactPersonInfoList(userId));
		
		return dto;
	}

    @Override
    public BasicInfoDTO getBasicInfo(String userId) {
        return tianchengDataService.getBasicInfo(userId);
    }

    /**
     * 查询用户天秤额度评分卡所需参数
     * @param userId
     * @param headFunctionCode
     * @return
     */
    @Override
    public CreditScoreCardDTO queryCreditScoreCardParam(String userId, String headFunctionCode) {
        return tianchengDataService.queryCreditScoreCardParam(userId, headFunctionCode);
    }

    public SocialSecurityTeCreditScoreCardDTO queryCreditScoreCardParamForSocialSecurityTe(String userId) {
        return tianchengDataService.queryCreditScoreCardParamForSocialSecurityTe(userId);
    }

    public CreditCardTeCreditScoreCardDTO queryCreditScoreCardParamForCreditCardTe(String userId) {
        return tianchengDataService.queryCreditScoreCardParamForCreditCardTe(userId);
    }
}
